<?php
header('Content-type:text/html;charset=utf-8');
/**
 * 更加经纬度获取城市名称
 * @param  string $lat 纬度
 * @param  string $lng经度
 * @return [
 *   'state'=>true成功 false 失败,
 *  'city_name'=>城市名称
 *   ];
 */
function get_baidu_map_by_lat_and_lng($lat,$lng)
{
    $params = http_build_query(array(
        "location" =>$lat.','.$lng,
        "output" => 'json',
        "pois" => 0,
        "ak" => "RYFWTygaasFLKFmFfz66s8BeuP89LVZH"
    ));
    $url = 'http://api.map.baidu.com/geocoder/v2/';
    $httpInfo = array();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'JuheData');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
    $response = curl_exec($ch);
    if ($response === FALSE) {
        return [
            'state' => false
        ];
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    $result = json_decode($response, true);
    $state = $result['status'];
    if ($state == 0) {
        $city_name = $result['result']['addressComponent']['city'];
        if ($city_name) {
            return [
                'state'=>true,
                'city_name'=>$city_name
            ];
        }
    }
    return [
        'state' => false
    ];

}
//************1.身份证信息查询************
$content = get_baidu_map_by_lat_and_lng(39.983424,116.322987);
var_dump($content);

